package com.qf.controller;

import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import com.qf.utils.SYS;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2021/10/21.
 */
@WebServlet("/user")
public class UserController extends BaseController {
    private UserService userService=new UserServiceImpl();

    //验证用户信息是否正确
    public String logout(HttpServletRequest request, HttpServletResponse response){
        //立即使session失效;
        HttpSession session = request.getSession();
        session.invalidate();// 使session失效
        return "controller:load";
    }
    public String login(HttpServletRequest request, HttpServletResponse response){
        //接收用户的昵称和密码
       String usrname=request.getParameter("usrname");
        String usrpass=request.getParameter("usrpass");
        //调用业务
        User user=userService.login(usrname,usrpass);
        if(user==null){
            //失败--返回login.jsp
            //携带错误信息
            request.setAttribute("message","信息填写有误，请重来一遍!");
            return "forward:login";
        }else{
            //成功--去首页
            //记录?????---session技术 ---session.setAttribute(名,值)
            //获取session
            HttpSession session=request.getSession();
            //存登录成功的用户信息
            session.setAttribute(SYS.USER,user);
            return "controller:load";//load--index.jsp
        }
    }
    public String getName(HttpServletRequest request, HttpServletResponse response){
        //接收页面
        String name=request.getParameter("name");
        if(userService.checkName(name))
            return "{\"code\":1}";
        return "{\"code\":0}";
    }



}
